class EpisodeGameResultTracker:
    """每局游戏统计跟踪器"""

    def __init__(self):
        self.total_wins = 0
        self.total_losses = 0
        self.total_draws = 0  # 平局数
        self.win_rate_history = []  # 总体胜率统计[episode,total_wins,total_win_rate]
        self.invalid_moves_per_episode = []  # 非法落子统计

    def update_game_result(self, winner: int, ai_player: int = 1) -> None:
        """更新游戏结果统计"""
        if winner == ai_player:
            self.total_wins += 1
        elif winner == -ai_player:
            self.total_losses += 1
        else:
            self.total_draws += 1

    def update_invalid_moves(self, invalid_moves_count: int) -> None:
        """更新非法落子统计"""
        self.invalid_moves_per_episode.append(invalid_moves_count)

    def get_total_games(self) -> int:
        """获取总游戏数"""
        return self.total_wins + self.total_losses + self.total_draws

    def get_total_win_rate(self) -> float:
        """获取胜率"""
        total_games = self.get_total_games()
        return self.total_wins / total_games if total_games > 0 else 0.0

    def get_total_draws_rate(self) -> float:
        """获取输棋率"""
        total_games = self.get_total_games()
        return self.total_draws / total_games if total_games > 0 else 0.0

    def get_total_losses_rate(self) -> float:
        """获取平局率"""
        total_games = self.get_total_games()
        return self.total_losses / total_games if total_games > 0 else 0.0

    def get_wins_in_last_batch(self) -> int:
        """计算最近批次的胜利数"""
        if len(self.win_rate_history) == 0:
            return self.total_wins
        return self.total_wins - self.win_rate_history[-1][1]
